#include "PScheduler.h"
#include "PlatformManager.h"
#include <Windows.h>
#include <WindowsX.h>

namespace Proton 
{
	namespace Managers
	{

		std::vector<IInitializable*> PScheduler::initializableModules;
		std::vector<ILoadable*> PScheduler::loadableModules;
		std::vector<IUpdateable*> PScheduler::updateableModules;
	    std::vector<IRenderable*> PScheduler::renderableModules;

		PScheduler::PScheduler(void)
		{
			pPlatform = NULL;
			pGraphics = NULL;
			_loop = true;
		}

		PScheduler::~PScheduler(void)
		{
		}

		void PScheduler::initialize(PlatformManager* _pPlatform, GraphicsManager* _pGraphics)
		{
			pPlatform = _pPlatform;
			pGraphics = _pGraphics;
		}
		
		void PScheduler::registerModule(IInitializable* module)
		{	
			if(module!=NULL)
				initializableModules.push_back(module);
		}

		void PScheduler::registerModule(IUpdateable* module)
		{	
			if(module!=NULL)
				updateableModules.push_back(module);
		}

		void PScheduler::registerModule(ILoadable* module)
		{	
			if(module!=NULL)
				loadableModules.push_back(module);
		}

		void PScheduler::registerModule(IRenderable* module)
		{	
			if(module!=NULL)
				renderableModules.push_back(module);
		}

		int PScheduler::loop()
		{			
			MSG msg = {0};
			while (_loop)
			{
				if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
				{
					TranslateMessage(&msg);
					DispatchMessage(&msg);

					if (msg.message == WM_QUIT)
					{
						quit();
						break;
					}
				}
				else
				{
					for (unsigned int i=0; i < updateableModules.size(); i++)
						updateableModules[i]->update();
					//render
					for (unsigned int i=0; i < renderableModules.size(); i++)
						renderableModules[i]->render();
				}
			}

			return 0;
		}

		void PScheduler::initializeModules()
		{
			for (unsigned int i=0; i < initializableModules.size(); i++)
						initializableModules[i]->initialize();
		}

		void PScheduler::loadContentOfModules()
		{
			for (unsigned int i=0; i < loadableModules.size(); i++)
						loadableModules[i]->load();
		}

		void PScheduler::quit()
		{
			_loop = false;
		}

	}
}